package com.deyuanyun.pic.settings.controller.prvlg;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.controller.dto.prvlg.ModifyState;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleEdit;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleGrant;
import com.deyuanyun.pic.settings.controller.dto.prvlg.RoleQuery;
import com.deyuanyun.pic.settings.controller.vo.RoleMenuVO;
import com.deyuanyun.pic.settings.controller.vo.RoleVO;
import com.deyuanyun.pic.settings.service.prvlg.RoleMgrService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限模块 “角色” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-23
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/settings/prvlg/role")
public class RoleMgrController {

	@Resource
	private RoleMgrService roleMgrService;

	/**
	 * 跳转到 “角色” 管理页面
	 *
	 * @return
	 */
	@RequestMapping("/index.html")
	public ModelAndView index(){
		return new ModelAndView("/settings/prvlg/roleMgr");
	}

	/**
	 * 条件 查询“角色”
	 *
	 * @param roleQuery
	 */
	@RequestMapping("/query.json")
	@ResponseBody
	public void query(RoleQuery roleQuery) {
		AjaxSupport.sendSuccessText("success", new RoleVO().fromDomainList(roleMgrService.query(roleQuery)));
	}

	/**
	 * 创建 或者 编辑“角色”
	 *
	 * @param roleEdit
	 */
	@RequestMapping("/edit.json")
	@ResponseBody
	public void edit(RoleEdit roleEdit) {
		Asserts.assertLogic(StringUtils.isNullEmpty(roleEdit.getCode()), "角色编号 不能为空");
		Asserts.assertLogic(StringUtils.isNullEmpty(roleEdit.getName()), "角色名称 不能为空");
		int updateRows = roleMgrService.edit(roleEdit.toDomain());
		AjaxSupport.sendSuccessText("success", updateRows);
	}

	/**
	 * 删除“角色”
	 *
	 * @param ids id字符串数组
	 */
	@RequestMapping("/delete.json")
	@ResponseBody
	public void delete(String[] ids) {
		Asserts.assertLogic(ids.length < 1, "请选择要删除的 角色");
		int updateRows = roleMgrService.delete(ids);
		AjaxSupport.sendSuccessText("success", updateRows);
	}

	/**
	 * 修改“角色”状态
	 *
	 * @param roles id字符串数组
	 */
	@RequestMapping("/modifyState.json")
	@ResponseBody
	public void modifyState(ModifyState roles) {
		int updateRows = roleMgrService.modifyState(roles);
		AjaxSupport.sendSuccessText("success", updateRows);
	}

	/**
	 * 查询可以授权的查单
	 */
	@RequestMapping("/queryMenu.json")
	@ResponseBody
	public void queryMenu(String roleId) {
		AjaxSupport.sendSuccessText("success", new RoleMenuVO().fromDomainList(roleMgrService.queryMenu(roleId)));
	}

	/**
	 * “角色”授权访问菜单
	 *
	 * @param roleGrant
	 */
	@RequestMapping("/grant.json")
	@ResponseBody
	public void grant(RoleGrant roleGrant) {
		int updateRows = roleMgrService.grant(roleGrant);
		AjaxSupport.sendSuccessText("success", updateRows);
	}

}
